#include<stdio.h>
#include <string.h>
struct student{    	   	   
    int id;          //学生学号
    char name[20];     //学生姓名
    int score;          //考试成绩
    struct student *next;
}    	   	   *head;
//函数声明
void creat();
void sort(); 
void output();
void select(int);
void Free( );
int main()
{    	   	   
    creat();//1
    sort(); //1
    output();//1
    int n;
    printf("请输入要查找学生编号：\n");
    scanf("%d",&n);//1
    select(n); //1
    Free();//1
    return 0;
}    	   	   
//创建单链表保存学生信息，并输出学生信息
void  creat(){    	   	   
    head = (struct student *)malloc(sizeof(struct student));//1
    head->next = NULL; //1
    int number,i;
    struct student *q ,*tail;//1
    tail = head;//1
    printf("请输入班级人数：\n");
    scanf("%d",&number); //1
    printf("请依次输入每个学生的学号 姓名 成绩\n");
    for( i = 0;i<number;i++)//1
    {    	   	   
        printf("请输入第%d个学生的信息：\n",i+1);
        q = (struct student *)malloc(sizeof(struct student));//1
        q->next = NULL;//1
        scanf("%d %s %d",&q->id,q->name,&q->score);//1
        tail->next = q; //1
        tail = q;
    }
 
 
  }
 
 
//按照学生的成绩分数降序，并输出学生信息
void sort(){    	   	   
    struct student* p = head->next; //1
    struct student* p_pre = p;
    while(p_pre!= NULL)//1
    {    	   	   
 
        p = p->next;//1
        while (p)
        {    	   	   
            if((p->score)>=p_pre->score)//1
 
            {   //直接交换两节点的值
                int temp_change = p->score;
                p->score = p_pre->score;
                p_pre->score = temp_change; //1
                int id = p->id;
                p->id = p_pre->id;
                p_pre->id = id; //1
                char c[20] ;
                strcpy(c, p->name);
                strcpy(p->name,p_pre->name);
                strcpy(p_pre->name,c);//1
 
            }
            p = p->next;//1
        }
        p_pre = p_pre->next;//1
        p = p_pre; //1
    }
 
}    	   	   
void output()
{    	   	   
    FILE * fp=fopen("stu.txt","w");//1
    struct student *out;
    //fprintf(fp,"排序后的学生信息\n");
    fprintf(fp,"id   name  score\n");
    out = head->next; //1
    while(out)//1
    {    	   	   
        fprintf(fp,"%d     %s   %d\n",out->id,out->name,out->score);//1
        out = out->next;//1
    }
}    	   	   
//查询学生信息
void select(int id){    	   	   
    struct student *p;
    p = head->next; //1
    int flag = 0;
    while(p)//1
    {    	   	   
        if(id ==p->id)//1
        {    	   	   
            flag = 1;//1
            break;
        }
        p=p->next;//1
    }
    if(flag)//1
    {    	   	   
        printf("该学生的学号为：%d  姓名：%s  成绩为：%d",p->id,p->name,p->score);
    }
    else
    {    	   	   
        printf("找不到");
    }
 
}    	   	   
void Free()
{    	   	   
 struct student *p=head,*pr=NULL;
 while(p!=NULL)//1
 {    	   	   
   pr=p;
   p=p->next;//1
   free(pr);//1
 }
}